package com.scpii.universal.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.scpii.universal.hibernate.dao.BaseDao;
import com.scpii.universal.hibernate.entity.OrderTab;
import com.scpii.universal.hibernate.intface.BaseInterface;
import com.scpii.universal.util.Const;
import com.scpii.universal.util.SQLs;
import com.scpii.universal.util.SpringBeanFactory;

public class OrderTabService implements BaseInterface<OrderTab> {
	private static final Logger log = LoggerFactory
			.getLogger(OrderTabService.class);
//	private BaseDao<OrderTab> orderTabDao = SpringBeanFactory.orderTabDao;
	private BaseDao<OrderTab> orderTabDao;
	
	public OrderTab get(Integer id) throws Exception {
		return orderTabDao.get(id);
	}

	public List<OrderTab> getAll() throws Exception {
		return orderTabDao.getAll();
	}

	public OrderTab get(String propertyName, Object value) throws Exception {
		return null;
	}

	public List<OrderTab> getList(String propertyName, Object value)
			throws Exception {
		return null;
	}

	public Long getTotalCount() throws Exception {
		return null;
	}

	public void save(OrderTab obj) throws Exception {
		orderTabDao.save(obj);
	}

	public void update(OrderTab obj) throws Exception {

	}

	public void delete(OrderTab obj) throws Exception {

	}

	public void delete(Integer id) throws Exception {

	}

	public List<OrderTab> hqlGetList(String hql, Map<String, Object> argsMap)
			throws Exception {
		return orderTabDao.hqlGetList(hql, argsMap);
	}

	public void hqlUpdate(String namedHqlQuery, Map<String, Object> argsMap)
			throws Exception {
		orderTabDao.hqlUpdate(namedHqlQuery, argsMap);
	}

	public Long hqlGetNums(String namedHqlQuery, Map<String, Object> argsMap)
			throws Exception {
		return orderTabDao.hqlGetNums(namedHqlQuery, argsMap);
	}

	public List<OrderTab> sqlGetList(String sqls, Map<String, Object> argsMap)
			throws Exception {
		return orderTabDao.sqlGetList(sqls, argsMap);
	}

	public void sqlUpdate(String sqls, Map<String, Object> argsMap)
			throws Exception {
		orderTabDao.sqlUpdate(sqls, argsMap);
	}

	public Long sqlGetNum(String sqls, Map<String, Object> argsMap)
			throws Exception {
		return orderTabDao.sqlGetNum(sqls, argsMap);
	}
	
	/**
	 * 根据用户ID，获得用户的订单列表
	 * @param userId
	 * @return
	 * @throws Exception
	 * @author 甘建新
	 * 2012-7-19上午02:56:48
	 */
	public List<OrderTab> getOrderListByUserId(int userId,int appId)throws Exception{
		if (userId>0 && appId>0) {
			Map<String, Object> argsMap = new HashMap<String, Object>();
			argsMap.put("userId", userId);
			argsMap.put("appId", appId);
			
			return this.hqlGetList("GET_ORDERLIST_BYUSERID", argsMap);
		}
		return null;
	}
	
	/**
	 * 
	 * @param userId
	 * @param appId
	 * @return
	 * @throws Exception
	 * @author 甘建新
	 * 2012-8-15下午10:44:40
	 */
	public Integer getOrderListPageCount(int userId,int appId)throws Exception{
		if (userId>0 && appId>0) {
			Map<String, Object> argsMap = new HashMap<String, Object>();
			argsMap.put("userId", userId);
			argsMap.put("appId", appId);
			
			Long resultCount = this.hqlGetNums("GET_ORDERLIST_COUNT_BYUSERID_APPID", argsMap);
			int result = resultCount.intValue();
			int pageCount = orderTabDao.getRecordPageNum(result, Const.MOBILE_CLIENT_NO_PERPAGE);
			
			return pageCount;
		}
		return 0;
	}
	
	/**
	 * 分页查询订单列表
	 * @param userId
	 * @param appId
	 * @param pageNum
	 * @return
	 * @throws Exception
	 * @author 甘建新
	 * 2012-8-15下午10:45:27
	 */
	public List<OrderTab> getOrderListByPageNo(int userId,int appId,int pageNum)throws Exception{
		if (userId>0 && appId>0) {
			List<OrderTab> orderList = new ArrayList<OrderTab>();
			
			int MAXROWNUM = pageNum * Const.MOBILE_CLIENT_NO_PERPAGE;
			int MINROWNUM = (pageNum - 1) * Const.MOBILE_CLIENT_NO_PERPAGE;
			
			Map<String, Object> argsMap = new HashMap<String, Object>();
			argsMap.put("userId", userId);
			argsMap.put("appId", appId);
			argsMap.put("MAXROWNUM", MAXROWNUM);
			argsMap.put("MINROWNUM", MINROWNUM);
			
			orderList = this.sqlGetList(SQLs.PUBLIC_STRS_HEAD, argsMap);
			return orderList;
		}
		return null;
	}

	public void setOrderTabDao(BaseDao<OrderTab> orderTabDao) {
		this.orderTabDao = orderTabDao;
	}
}